package linkedlist;

public class LinkedList<E> {

    private class Node{
        public E e;
        public Node next;

        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e,null);
        }

        public Node(){
            this(null,null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }


    //虚拟头节点
    private Node dummyHead;
    private int size;

    public LinkedList(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    public LinkedList(int[] arr){

    }

    //获取链表中的元素个数
    public int getSize(){
        return size;
    }

    //返回链表是否为空
    public boolean isEmpty(){
        return size == 0;
    }



    //指定位置添加元素
    public void add(int index,E e){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("");
        }
            Node prev = dummyHead;
            for(int i = 0 ; i < index ; i++){
                prev = prev.next;
            }

            /*Node node = new Node(e);
            node.next = prev.next;
            prev.next = node;*/
            prev.next = new Node(e);
            size++;
    }

    //添加元素到链表头
    public void addFirst(E e){
        add(0,e);
        size++;
    }

    //在链表末尾添加元素E
    public void addLast(E e){
        add(size,e);
    }

    //
    public E get(int index){
        //判断index的合法性
        Node current = dummyHead.next;
        for(int i = 0 ; i < index ; i++){
            current = current.next;
        }

        return current.e;
    }

    //获取链表第一个元素
    public E getFirst(){
        return get(0);
    }

    //获取链表最后一个元素
    public E getLast(){
        return get(size - 1);
    }

    public void set(int index,E e){
        //判断index合法性
        Node current = dummyHead.next;
        for(int i = 0 ; i < index ; i++){
            current = current.next;
        }
        current.e = e;
    }

    //查找链表中是否存在元素e
    public boolean contains(E e){
        Node current = dummyHead.next;
        while (current != null){
            if(current.e == e){
                return true;
            }
            current = current.next;
        }
        return false;
    }

    //
    public Node remove(int index){
        //判断index合法性
        Node prev = dummyHead;
        for(int i = 0 ; i < index ; i++){
            prev = prev.next;
        }

        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return  retNode;
    }

    public E removeFirst(){
       Node node =  remove(0);
       return node.e;
    }

    //删除指定元素
    public void removeElement(E e){

        Node prev = dummyHead;
        while (prev.next != null){
            if(prev.next.e.equals(e)){
                break;  //跳出循环，执行step2
            }
            prev = prev.next;
        }

        //step2
        if(prev.next != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
        }
    }




























}
